/******************************************************************************/
/*  mbutils.h                                                                 */
/*                                                                            */
/*  FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. */
/*  Copyright (c) 2006 Christian Walter <wolti@sil.at>                        */
/*  All rights reserved.                                                      */
/******************************************************************************/

#ifndef __MB_UTILS_H
#define __MB_UTILS_H



#ifdef __cplusplus
  PR_BEGIN_EXTERN_C
#endif



/*  This module contains some utility functions which can be used by
 *  the application. It includes some special functions for working with
 *  bitfields backed by a character array buffer.
 */


/*  Function to set bits in a byte buffer.
 *
 *  This function allows the efficient use of an array to implement bitfields.
 *  The array used for storing the bits must always be a multiple of two
 *  bytes. Up to eight bits can be set or cleared in one operation.
 *
 *  _pByteBuf - a buffer where the bit values are stored. Must be a
 *   multiple of 2 bytes. No length checking is performed and if
 *   (_bitOffset / 8) is greater than the size of the buffer memory contents
 *   is overwritten.
 *  _bitOffset - the starting address of the bits to set. The first
 *   bit has the offset 0.
 *  _numBits - number of bits to modify. The value must always be smaller
 *   than 8.
 *  _values - thew new values for the bits. The value for the first bit
 *   starting at _bitOffset is the LSB of the value _values.
 *
 *  bits[2] = {0, 0};
 *
 *  // Set bit 4 to 1 (read: set 1 bit starting at bit offset 4 to value 1)
 *  xMbUtilSetBits(bits, 4, 1, 1);
 *
 *  // Set bit 7 to 1 and bit 8 to 0.
 *  xMbUtilSetBits(bits, 7, 2, 1);
 *
 *  // Set bits 8 - 11 to 0x05 and bits 12 - 15 to 0x0A;
 *  xMbUtilSetBits(bits, 8, 8, 0x5A);
 */
void xMbUtilSetBits(uint8_t * _pByteBuf,
                    const uint16_t _bitOffset,
                    const uint8_t _numBits,
                    const uint8_t _values );



/*  Function to read bits in a byte buffer.
 *
 *  This function is used to extract up bit values from an array. Up to eight
 *  bit values can be extracted in one step.
 *
 *  _byteBuf - a buffer where the bit values are stored.
 *  _bitOffset - the starting address of the bits to set. The first
 *   bit has the offset 0.
 *  _numBits - number of bits to modify. The value must always be smaller
 *   than 8.
 *
 *  uint8_t bits[2] = {0, 0};
 *  uint8_t result;
 *
 *  // Extract the bits 3 - 10.
 *  result = xMbUtilGetBits(bits, 3, 8);
 */
uint8_t xMbUtilGetBits(uint8_t * _pByteBuf,
                       const uint16_t _bitOffset,
                       const uint8_t _numBits);


eMB_EXCEPTION prveMbError2Exception(const eMB_ERROR_CODE _errorCode);



#ifdef __cplusplus
  PR_END_EXTERN_C
#endif


#endif